const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


import { JqList } from 'jqList_model.js';
var jqlist = new JqList();

function selectNav() {
  //下拉菜单
  return ['hidden', 'hidden'];
}

var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');

var qqmapsdk;
var _source = '';
var searchRequest;
Page({
  data: {
    cityData: "全国",
    list_num1: "",
    //一级菜单
    firstPerson1: '9.9特价秒杀',
    firstPerson2: '综合排序',
    MBFlag: "true",
    scrollLeft: "",
    _num: '',
    //二级菜单
    tab_num: "",
    //二级菜单
    selectnav: selectNav(),
    searchFlag: true,
    //搜索
    mengbanFlag: true,
    //蒙版
    SearchClose: true,
    //关闭搜索按钮
    SearchTo: false,
    //搜索按钮
    kefuphoneArr: []
  },
  onLoad: function (options) {
    console.log(options);
    this.getwxcity();
    tt.setStorageSync('options', options); //判断订单来源是否为公众号

    if (options.source) {
      var source = options.source;
      tt.setStorageSync('source', source);
    } //显示分类与地区


    if (options.styleid2) {
      var _city = options.c_ity;
      var _typeid = options.t_ypeid;
      var styleid2 = options.styleid2;
      var list_num1 = options.list_num1;
      var _cateId = 0;
    } else {
      var _city = tt.getStorageSync("_city");
      var _typeid = tt.getStorageSync("t_ypeid");
      var _cateId = tt.getStorageSync("_cateId");
      _source = '首页';
      console.log('53', _city, _typeid, _cateId);
    }

    if (_cateId) {
      _cateId = _cateId;
    } else {
      _cateId = '0';
    }

    if (_cateId == 25) {
      console.log('_cateId == 25');
      var id = '0_0_1_0';
    } else if (_typeid != '0' && _cateId == '0') {
      var id = _typeid + '_' + styleid2 + '_0_0';

      if (_typeid == '') {
        id = '0_0_0_0';
        console.log(11, id);
      }
    } else if (styleid2 && _cateId == '0') {
      console.log("styleid2 && _cateId == ''");

      for (var i = 0; i <= 12; i++) {
        //判断传的值等于哪个下标使页面向左滚动到具体的距离
        if (i == list_num1) {
          var _width = 750 / 15 * i;

          this.setData({
            scrollLeft: _width,
            list_num1: styleid2
          });
        }
      }

      var _city = _city;
      id = _typeid + '_' + styleid2 + '_0_0';
    } else if (_cateId != '0') {
      console.log("_cateId != '0'");
      var _city = '全国';
      id = '0_' + _cateId + '_0_0';
    } else {
      var _city = '全国';
      var id = '0_0_0_0';
    }

    if (_city == '') {
      var _city = '全国';
    }

    var _cateList = Number(tt.getStorageSync('_cateList')); //获取首页分类传的参数滚动到具体某个分类
    console.log(22, _cateList);

    if (_cateList == 5) {
      //显示特价区域
      this.setData({
        tab_num: 31,
        list_num1: 0
      });
    } else {
      for (var i = 0; i <= 12; i++) {
        //判断传的值等于哪个下标使页面向左滚动到具体的距离
        if (i == _cateList) {
          var _width = 750 / 15 * i;

          this.setData({
            scrollLeft: _width
          });
          break;
        }
      }

      this.setData({
        list_num1: _cateList
      });
      console.log(this.data.list_num1, _width, this.data.scrollLeft);
    }

    var yeshu = '1';
    var styleid1 = id.split('_')[0];
    var styleid2 = id.split('_')[1];
    var styleid3 = id.split('_')[2];
    var styleid4 = id.split('_')[3];
    tt.setStorageSync('yeshu', yeshu);
    tt.setStorageSync('styleid1', styleid1);
    tt.setStorageSync('styleid2', styleid2);
    tt.setStorageSync('styleid3', styleid3);
    tt.setStorageSync('styleid4', styleid4);
    this.data.yeshu = yeshu;
    this.data.styleid1 = styleid1;
    this.data.styleid2 = styleid2;
    this.data.styleid3 = styleid3;
    this.data.styleid4 = styleid4;

    if (_city == '全国') {
      _city = tt.getStorageSync("wxcity");
    }

    this.setData({
      'styleid1': styleid1,
      'styleid2': styleid2,
      'styleid3': styleid3,
      'styleid4': styleid4,
      'cityData': _city,
      'All': true
    }); 
    // 获取客服手机号
    let kefuphone = tt.getStorageSync('kefuphoneArr');
    if (!kefuphone) {
      util.kefuphone();
    }
    this.setData({
      kefuphoneArr: kefuphone.slice(0, 2)
    });
  },
  //关闭页面时隐藏城市弹框,关闭一级和二级菜单
  onHide: function () {
    //点击关闭二级列表
    var newselect = selectNav();
    newselect[0] = 'hidden';
    newselect[1] = 'hidden';

    var _city = tt.getStorageSync("wxcity");

    if (!_city) {
      _city = this.data.cityData;
    }

    this.setData({
      searchFlag: true,
      //关闭搜索列表
      value: "",
      //清空输入框的值
      SearchClose: true,
      SearchTo: false,
      firstPerson2: '综合排序',
      //二级菜单默认显示全部时间
      selectnav: newselect,
      //隐藏二级列表
      MBFlag: true,
      //关闭蒙版
      'list_num1': "",
      //一级菜单默认为空
      tab_num: "",
      //二级菜单默认为空
      scrollLeft: 0 //一级菜单左侧位移为0

    });
    tt.removeStorageSync("t_ypeid"); //清空缓存
    tt.removeStorageSync("styleid1"); //清空缓存
    tt.removeStorageSync("styleid2"); //清空缓存
    tt.removeStorageSync("styleid3"); //清空缓存
    tt.removeStorageSync("styleid4"); //清空缓存
    tt.removeStorageSync("_city"); //清空缓存
    tt.removeStorageSync("_cateList"); //清空缓存
    tt.removeStorageSync("_cateId"); //清空缓存
    // var options = wx.getStorageSync('options');
    //this.onLoad(options); //打开页面重新加载
  },
  _onLoad: function () {
    var latitude = tt.getStorageSync("latitude");
    var longitude = tt.getStorageSync("longitude");
    
    if (tt.getStorageSync("_city")){
      var wxcity=tt.getStorageSync("_city")
    }else{
      var wxcity = tt.getStorageSync("wxcity");
    }
    console.log(_source);

    if (_source == '首页') {
      var wxprovince = tt.getStorageSync("wxprovince");
      jqlist.getJqlist(this.data.styleid1, this.data.styleid2, this.data.styleid3, this.data.styleid4, this.data.yeshu, wxcity, wxprovince, latitude, longitude, res => {
        tt.setStorageSync('res10', res);
        this.setData({
          'Jqlist': res
        });
      });
    } else {
      jqlist.getJqlist(this.data.styleid1, this.data.styleid2, this.data.styleid3, this.data.styleid4, this.data.yeshu, wxcity, wxprovince, latitude, longitude, res => {
        tt.setStorageSync('res10', res);
        this.setData({
          'Jqlist': res
        });
      });
    }

    jqlist.getShengHuiRow(res => {
      this.setData({
        'ShengHui': res
      });
    });
  },
  //打开选择城市
  onChangeCity: function (e) {
    this.setData({
      tab_num: "",
      //清空一级列表
      _num: "",
      //清空二级级列表
      firstPerson2: '综合排序',
      //二级列表默认为综合排序
      'list_num1': "",
      //清空滚动菜单
      scrollLeft: 0 //滚动菜单左侧位移为0

    });
  },
  //选择城市
  citySelect: function (e) {
    var latitude = tt.getStorageSync("latitude");
    var longitude = tt.getStorageSync("longitude");
    var _city = e.target.dataset.city;
    var styleid = e.target.dataset.styleid;
    var yeshu = '1';
    var styleid1 = styleid.split('_')[0];
    var styleid2 = styleid.split('_')[1];
    var styleid3 = styleid.split('_')[2];
    var styleid4 = styleid.split('_')[3];
    tt.setStorageSync('yeshu', yeshu);
    tt.setStorageSync('styleid1', styleid1);
    tt.setStorageSync('styleid2', styleid2);
    tt.setStorageSync('styleid3', styleid3);
    tt.setStorageSync('styleid4', styleid4);
    this.data.yeshu = yeshu;
    this.data.styleid1 = styleid1;
    this.data.styleid2 = styleid2;
    this.data.styleid3 = styleid3;
    this.data.styleid4 = styleid4;
    this.setData({
      'styleid1': styleid1,
      'styleid2': styleid2,
      'styleid3': styleid3,
      'styleid4': styleid4
    });

    if (_source == '首页') {
      var wxcity = tt.getStorageSync("wxcity");
      var wxprovince = tt.getStorageSync("wxprovince");
      this.setData({
        cityData: wxcity
      });
    }

    this.setData({
      cityData: _city
    });
    jqlist.getJqlist(this.data.styleid1, this.data.styleid2, this.data.styleid3, this.data.styleid4, this.data.yeshu, wxcity, wxprovince, latitude, longitude, res => {
      tt.setStorageSync('res10', res);
      this.setData({
        'Jqlist': res,
        'All': true,
        scrollLeft: 0,
        //一级菜单左侧位移为0
        firstPerson1: '综合排序'
      });
    });
  },
  //使用输入框搜索
  searchValue: function (e) {
    var _data = e.detail.value;
    this.data.SearchValue = e.detail.value;
    var patt3 = /^[\u4e00-\u9fa5]{2,20}$/; //正则验证汉字不能少两个，不能多余20个

    this.setData({
      value: e.detail.value //保存输入框的值

    });
    this.data.datarow = _data;
    clearTimeout(searchRequest);
    searchRequest = setTimeout(() => {
      if (_data == '') {
        this.setData({
          searchFlag: true,
          //关闭搜索蒙版
          SearchClose: true,
          SearchTo: false,
          //打开搜索图标
          value: "" //清空输入框的值

        });
      } else if (patt3.test(_data)) {
        //搜索页面
        jqlist.getSearchFor(this.data.datarow, res => {
          this.setData({
            'SearchFor': res,
            searchFlag: false,
            SearchClose: false,
            SearchTo: true
          });
        });
      }
    }, 1000);
  },
  //使用回车键搜索
  searchEnter: function () {
    this.setData({
      searchFlag: true,
      //关闭蒙版
      SearchClose: true,
      SearchTo: false,
      value: "" //清空输入框的值

    });
    var inputdata = this.data.SearchValue;

    if (inputdata) {
      tt.navigateTo({
        url: '../ycSearch/ycSearch?inputdata=' + inputdata + '&jqyc=景区'
      });
    }
  },
  //点击关闭按钮关闭搜索框
  toClose: function () {
    this.setData({
      searchFlag: true,
      SearchClose: true,
      SearchTo: false,
      value: "" //清空输入框的值

    });
  },
  //关闭搜索
  onCloseSearch: function (e) {
    this.setData({
      searchFlag: true,
      SearchClose: true,
      SearchTo: false,
      value: "" //清空输入框的值

    });
  },
  listClick: function (e) {
    var newselect = selectNav();
    var latitude = tt.getStorageSync("latitude");
    var longitude = tt.getStorageSync("longitude");
    newselect[0] = 'hidden';
    newselect[1] = 'hidden';
    var styleid = e.target.dataset.styleid;
    var yeshu = '1';
    var styleid1 = styleid.split('_')[0];
    var styleid2 = styleid.split('_')[1];
    var styleid3 = styleid.split('_')[2];
    var styleid4 = styleid.split('_')[3];
    tt.setStorageSync('yeshu', yeshu);
    tt.setStorageSync('styleid1', styleid1);
    tt.setStorageSync('styleid2', styleid2);
    tt.setStorageSync('styleid3', styleid3);
    tt.setStorageSync('styleid4', styleid4);
    this.data.yeshu = yeshu;
    this.data.styleid1 = styleid1;
    this.data.styleid2 = styleid2;
    this.data.styleid3 = styleid3;
    this.data.styleid4 = styleid4;
    this.setData({
      'styleid1': styleid1,
      'styleid2': styleid2,
      'styleid3': styleid3,
      'styleid4': styleid4,
      selectnav: newselect,
      'All': true,
      list_num1: e.target.dataset.num,
      MBFlag: true,
      //关闭蒙版
      tab_num: "",
      //清空一级列表
      _num: "",
      //清空二级级列表
      firstPerson2: '综合排序'
    });

    if (_source == '首页') {
      var wxcity = tt.getStorageSync("wxcity");
      var wxprovince = tt.getStorageSync("wxprovince");
    }

    jqlist.getJqlist(this.data.styleid1, this.data.styleid2, this.data.styleid3, this.data.styleid4, this.data.yeshu, wxcity, wxprovince, latitude, longitude, res => {
      tt.setStorageSync('res10', res);
      this.setData({
        'Jqlist': res
      });
    }); //点击关闭二级列表
  },
  clickTab: function (e) {
    //点击列表切换
    var index = parseInt(e.currentTarget.dataset.index);
    var newselect = selectNav();

    if (this.data.selectnav[index] == 'hidden') {
      newselect[index] = 'show';
      this.setData({
        MBFlag: false,
        //打开蒙版
        tab_num: e.target.dataset.tabnum
      });
    } else {
      newselect[index] = 'hidden';
      this.setData({
        MBFlag: true,
        //关闭蒙版
        tab_num: e.target.dataset.tabnum
      });
    } //设置为新的数组


    this.setData({
      selectnav: newselect
    });
  },
  clickTab1: function (e) {
    var styleid = e.target.dataset.styleid;
    var latitude = tt.getStorageSync("latitude");
    var longitude = tt.getStorageSync("longitude");
    var yeshu = '1';
    var styleid1 = styleid.split('_')[0];
    var styleid2 = styleid.split('_')[1];
    var styleid3 = styleid.split('_')[2];
    var styleid4 = styleid.split('_')[3];
    tt.setStorageSync('yeshu', yeshu);
    tt.setStorageSync('styleid1', styleid1);
    tt.setStorageSync('styleid2', styleid2);
    tt.setStorageSync('styleid3', styleid3);
    tt.setStorageSync('styleid4', styleid4);
    this.data.yeshu = yeshu;
    this.data.styleid1 = styleid1;
    this.data.styleid2 = styleid2;
    this.data.styleid3 = styleid3;
    this.data.styleid4 = styleid4;
    this.setData({
      'styleid1': styleid1,
      'styleid2': styleid2,
      'styleid3': styleid3,
      'styleid4': styleid4,
      _num: ""
    });

    if (_source == '首页') {
      var wxcity = tt.getStorageSync("wxcity");
      var wxprovince = tt.getStorageSync("wxprovince");
    }

    jqlist.getJqlist(this.data.styleid1, this.data.styleid2, this.data.styleid3, this.data.styleid4, this.data.yeshu, wxcity, wxprovince, latitude, longitude, res => {
      tt.setStorageSync('res10', res);
      this.setData({
        'Jqlist': res
      });
    });
    var index = parseInt(e.currentTarget.dataset.index);
    var newselect = selectNav();

    if (this.data.selectnav[index] == "hidden") {
      newselect[index] = "show";
      this.setData({
        MBFlag: true,
        tab_num: e.target.dataset.tabnum
      });
    } else {
      newselect[index] = "hidden";
      this.setData({
        MBFlag: true,
        tab_num: e.target.dataset.tabnum
      });
    }

    this.setData({
      selectnav: newselect
    });
  },
  mySelect: function (e) {
    //点击列表选择
    var styleid = e.target.dataset.styleid;
    var latitude = tt.getStorageSync("latitude");
    var longitude = tt.getStorageSync("longitude");
    var yeshu = '1';
    var styleid1 = styleid.split('_')[0];
    var styleid2 = styleid.split('_')[1];
    var styleid3 = styleid.split('_')[2];
    var styleid4 = styleid.split('_')[3];
    tt.setStorageSync('yeshu', yeshu);
    tt.setStorageSync('styleid1', styleid1);
    tt.setStorageSync('styleid2', styleid2);
    tt.setStorageSync('styleid3', styleid3);
    tt.setStorageSync('styleid4', styleid4);
    this.data.yeshu = yeshu;
    this.data.styleid1 = styleid1;
    this.data.styleid2 = styleid2;
    this.data.styleid3 = styleid3;
    this.data.styleid4 = styleid4;
    this.setData({
      'styleid1': styleid1,
      'styleid2': styleid2,
      'styleid3': styleid3,
      'styleid4': styleid4
    });

    if (_source == '首页') {
      var wxcity = tt.getStorageSync("wxcity");
      var wxprovince = tt.getStorageSync("wxprovince");
    }

    jqlist.getJqlist(this.data.styleid1, this.data.styleid2, this.data.styleid3, this.data.styleid4, this.data.yeshu, wxcity, wxprovince, latitude, longitude, res => {
      tt.setStorageSync('res10', res);
      this.setData({
        'Jqlist': res
      });
    });
    var index = parseInt(e.currentTarget.dataset.index);
    var newselect = selectNav();
    newselect[index] = 'hidden';
    var index2 = e.currentTarget.dataset.index.split('-')[0];

    if (index2 == 0) {
      this.setData({
        selectnav: newselect,
        firstPerson1: e.target.dataset.text,
        MBFlag: true,
        //关闭蒙版
        _num: e.target.dataset.num
      });
    } else if (index2 == 1) {
      this.setData({
        selectnav: newselect,
        firstPerson2: e.target.dataset.text,
        MBFlag: true,
        //关闭蒙版
        _num: e.target.dataset.num
      });
    }
  },
  tapMenuHide: function (e) {
    //点击蒙版关闭蒙版和列表
    var index = parseInt(e.currentTarget.dataset.index);
    var newselect = selectNav();
    newselect[index] = 'hidden';
    this.setData({
      selectnav: newselect,
      MBFlag: true //关闭蒙版

    });
  },
  // 拨打电话
  calling: function () {
    tt.makePhoneCall({
      phoneNumber: '188-6035-1905'
    });
  },
  //分享
  onShareAppMessage: function () {
    util.sharePage();
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  //回到顶部
  goTop: function () {
    // 一键回到顶部
    util.goTop();
  },
  //上拉加载
  onReachBottom: function (e) {
    var yeshu = tt.getStorageSync("yeshu");
    var latitude = tt.getStorageSync("latitude");
    var longitude = tt.getStorageSync("longitude");
    var yeshu2 = Number(yeshu) + Number(1);
    tt.setStorageSync('yeshu', yeshu2);
    this.data.yeshu = yeshu2;
    var styleid1 = tt.getStorageSync("styleid1");
    var styleid2 = tt.getStorageSync("styleid2");
    var styleid3 = tt.getStorageSync("styleid3");
    var styleid4 = tt.getStorageSync("styleid4");
    this.data.styleid1 = styleid1;
    this.data.styleid2 = styleid2;
    this.data.styleid3 = styleid3;
    this.data.styleid4 = styleid4;

    if (_source == '首页') {
      var wxcity = tt.getStorageSync("wxcity");
      var wxprovince = tt.getStorageSync("wxprovince");
    }

    tt.showLoading({
      title: '加载中'
    });
    jqlist.getJqlist(this.data.styleid1, this.data.styleid2, this.data.styleid3, this.data.styleid4, this.data.yeshu, wxcity, wxprovince, latitude, longitude, res => {
      if (res != '') {
        var res10 = tt.getStorageSync("res10");
        var res5 = res10.concat(res);
        tt.setStorageSync('res10', res5);
        this.setData({
          'Jqlist': res5,
          'styleid1': this.data.styleid1,
          'styleid2': this.data.styleid2,
          'styleid3': this.data.styleid3,
          'All': true
        });
      } else {
        tt.showLoading({
          title: '已经加载完了'
        });
        this.setData({
          'All': false
        });
      } //显示.5秒隐藏加载提示


      setTimeout(function () {
        tt.hideLoading();
      }, 500);
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onUnload: function () {},
  //获取微信定位位置
  getwxcity: function () {
    qqmapsdk = new QQMapWX({
      key: '5XNBZ-SFPRU-M35VS-BNF67-DIHHJ-AEFD7' //这里自己的key秘钥进行填充

    }); //获取当时用户小程序设置（权限设置状态）

    tt.getSetting({
      success: res => {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          this.getLocation();
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          this.getLocation();
        } else {
          this.getLocation();
        }
      }
    });
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    tt.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        vm.getLocal(latitude, longitude);
        tt.setStorageSync('latitude', latitude); //纬度

        tt.setStorageSync('longitude', longitude); //经度
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res));
        tt.setStorageSync('latitude', ''); //纬度

        tt.setStorageSync('longitude', ''); //经度

        vm._onLoad();
      }
    });
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        let province = res.result.ad_info.province;
        let city = res.result.ad_info.city; //设置缓存（吧用户地址信息存入缓存）

        tt.setStorageSync('wxcity', city);
        tt.setStorageSync('wxprovince', province);
        vm.setData({
          province: province,
          city: city,
          latitude: latitude,
          longitude: longitude
        });

        vm._onLoad();
      },
      fail: function (res) {
        console.log("error");

        vm._onLoad();
      },
      complete: function (res) {}
    });
  }
});